//
//  MeetingPasswordViewController.swift
//  onemeetingmac
//
//  Created by 张衡 on 2020/11/26.
//  Copyright © 2020 onemeeting. All rights reserved.
//

import Foundation

class MeetingPasswordViewController: NSViewController {
    
    var closeBlock:os_block_t?
    var nextBlock:ResultString?
    
    override func loadView() {
        let view = NSView.init(frame: NSMakeRect(0, 0, 400, 180))
        view.wantsLayer = true
        view.layer?.backgroundColor = NSColor.init(white: 237.0/255.0, alpha: 1.0).cgColor
        self.view = view
    }
    
    override func viewWillAppear() {
        super.viewWillAppear()
        self.refactoringWindow()
    }
    
    func refactoringWindow() {
        self.view.window?.title = "会议提示"
        self.view.window?.styleMask = [.titled,.fullSizeContentView,.closable]
        self.view.window?.collectionBehavior = NSWindow.CollectionBehavior.fullScreenNone
        self.view.window?.backingType = .buffered
        let closeButton = self.view.window?.standardWindowButton(.closeButton)
        closeButton?.target = self
        closeButton?.action = #selector(MeetingPasswordViewController.closeButtonAction)
        
    }
    
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.setupUI()
    }
    
    func setupUI() {
        self.view.addSubview(textField)
        textField.snp.makeConstraints { (make) in
            make.width.equalTo(200)
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(10)
            make.height.equalTo(20)
        }
        
        self.view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { (make) in
            make.height.equalTo(20)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(textField.snp.top).offset(-20)
        }
        
        self.view.addSubview(nextButton)
        nextButton.snp.makeConstraints { (make) in
            make.width.equalTo(80)
            make.right.equalTo(textField)
            make.top.equalTo(textField.snp.bottom).offset(20)
        }
        self.nextButton.becomeFirstResponder()
    }
    
    lazy var nextButton: NSButton = {
        let btn = NSButton.init(title: "确认", target: self, action: #selector(nextButtonAction(sender:)))
        btn.bezelStyle = .rounded
        btn.keyEquivalent = "\r"
        btn.focusRingType = .exterior
        return btn
    }()
    
    lazy var titleLabel: NSTextField = {
        let view = NSTextField.init(frame: NSRect.zero)
        view.maximumNumberOfLines = 1
        view.wantsLayer = true
        view.isBordered = false
        view.isEditable = false
        view.focusRingType = .none
        view.backgroundColor = NSColor.clear
        view.font = NSFont.systemFont(ofSize: 14)
        view.textColor = color_gray(120)
        view.stringValue = "请输入会议密码"
        view.sizeToFit()
        return view
    }()
    
    lazy var textField: NSTextField = {
        let view = NSTextField.init(frame: NSRect.zero)
        view.maximumNumberOfLines = 1
        view.wantsLayer = true
        view.backgroundColor = color_gray(255)
        view.font = NSFont.systemFont(ofSize: 14)
        view.textColor = color_gray(40)
        view.placeholderString = "请输入"
        view.isBordered = false
        view.alignment = .center
        view.cell?.lineBreakMode = .byTruncatingTail
        view.cell?.truncatesLastVisibleLine = true
        view.cell?.usesSingleLineMode = true
        view.layer?.borderWidth = 1
        view.layer?.borderColor = color_main140.cgColor
        view.focusRingType = .none
        return view
    }()
    
    @objc func nextButtonAction(sender:NSButton) {
        if self.nextBlock != nil {
            let string = self.textField.stringValue
            self.nextBlock!(string)
        }
    }
    
    @objc func closeButtonAction() {
        if self.closeBlock != nil {
            self.closeBlock!()
        }
        self.view.window?.close()
    }
}
